package com.ruoyi.modeling.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.modeling.service.IBuildTableService;


/**
 * 通用表数据Controller
 *
 * @author zhouwc
 * @createTime 2022/8/10 15:07
 */
@RestController
@RequestMapping("/modeling/table")
public class ModelingTableController extends BaseController {

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected IBuildTableService buildTableService;

    /**
     * 查询指定表数据列表
     */
    @GetMapping("/list/{table}")
    public TableDataInfo list(@PathVariable String table) {
        //TODO 只允许查询在线建表的数据

        Map<String, String> paramMap = new HashMap<String, String>();
        Map<String, String[]> map=request.getParameterMap();
        for(String key :map.keySet()) {
            //TODO 只添加表单存在的字段,否则拼接查询条件会包含不存在字段导致报错
            paramMap.put(StringUtils.toUnderScoreCase(key), map.get(key)[0]);
        }
        paramMap.remove("page_size");
        paramMap.remove("page_num");
        return buildTableService.queryPageList(table,paramMap);
    }
    /**
     * 查询指定表数据列表
     */
    @GetMapping("/get/{table}/{id}")
    public AjaxResult getById(@PathVariable("table") String tableName,@PathVariable Long id) {
        //TODO 只允许查询在线建表的数据
        Map<String,String> map = buildTableService.selectById(tableName,id);
        return AjaxResult.success(map);
    }


}
